package com.ccx.coachsite.ui.login;

import org.json.JSONObject;

import com.ccx.coachsite.CoachSiteMainActivity;
import com.ccx.coachsite.MainMenuActivity;
import com.ccx.coachsite.MainSplash;
import com.ccx.coachsite.bus.ILoadBus;
import com.ccx.coachsite.bus.LoadDataBus;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.http.BaseResp;
import com.ccx.coachsite.http.HttpRequest;
import com.ccx.coachsite.http.UrlConfig;
import com.ccx.coachsite.util.Log;
import com.ccx.coachsite.util.SharedPreferencesUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.Toast;

public class WeiboBase extends AsyncTask<Integer, Integer, Integer> implements ILoadBus {
	protected Activity superActivity;
	
	// !!!请根据您的实际情况修改!!! 认证成功后浏览器会被重定向到这个url中 本例子中不需改动
	protected String oauthCallback = "null";
	// !!!请根据您的实际情况修改!!! 换为您为自己的应用申请到的APP KEY
	protected String oauthConsumeKey = "801115505";
	// !!!请根据您的实际情况修改!!! 换为您为自己的应用申请到的APP SECRET
	protected String oauthConsumerSecret = "be1dd1410434a9f7d5a2586bab7a6829";
	
	protected int loginType;
	
	protected String weiboId;

	protected String weiboName;
	
	protected String weiboHeader;
	
	protected String weiboTitle = "";
	
	protected ProgressDialog mProgress;
	protected String processInfo;
	protected String successInfo;
	protected String errorInfo;
	private Intent data;
	private String userId;
	private String mobile;
	private boolean isShowProcess;
	int processtype = 0;
	
	public WeiboBase(Activity superActivity){
		super();
		isShowProcess = true;
		this.superActivity = superActivity;
		processInfo = "载入中...";
		processtype = 0;
	}
	
	public static void saveWeiboInfo(Context context, int weiboType,String weiboId, String weiboName)
	{
		SharedPreferencesUtil.setValue(context,
				SharedKey.Shared_Login_Type, weiboType+"");
		SharedPreferencesUtil.setValue(context,
				SharedKey.Shared_Weibo_Id, weiboId+"");
		SharedPreferencesUtil.setValue(context,
				SharedKey.Shared_Weibo_Name, weiboName+"");
	}
	
	public boolean isShowProcess() {
		return isShowProcess;
	}

	public void setShowProcess(boolean isShowProcess) {
		this.isShowProcess = isShowProcess;
	}
	
	public String getWeiboId() {
		return weiboId;
	}
	
	@Override
	protected Integer doInBackground(Integer... params) {
		if(isShowProcess)
			doQAuthResultTask();
		else
		{
			doWeiboLoginTask();
		}
		return 1;
	}

	public String getWeiboName() {
		return weiboName;
	}

	public String getWeiboHeader() {
		return weiboHeader;
	}
	
	public boolean doQAuth()
	{
		return false;
	}
	
	public boolean onQAuthResult(Intent data)
	{
		return false;
	}
	
	public String getUrl()
	{
		if(loginType == 2)
		{
			return UrlConfig
					.getUrlByKey(UrlConfig.QQLoginUrlKey);
		}
		else if(loginType == 1)
		{
			return UrlConfig
					.getUrlByKey(UrlConfig.SinaLoginUrlKey);
		}
		return "";
	}
	
	public void doWeiboLogin()
	{
		isShowProcess = false;
		weiboId = SharedPreferencesUtil.getValue(superActivity, SharedKey.Shared_Weibo_Id, "");
		weiboName = SharedPreferencesUtil.getValue(superActivity, SharedKey.Shared_Weibo_Name, "");
		this.execute();
	}
	
	public void doWeiboLoginTask()
	{
		boolean result = checkWeiboLogin();
		if(!result)
		{
			if(!isShowProcess)
			{
				
				//Toast.makeText(superActivity, "登陆失败", Toast.LENGTH_SHORT).show();
				SharedPreferencesUtil.setValue(superActivity,
						SharedKey.Shared_UserId_Key, "");
				Intent intent = new Intent(superActivity, CoachSiteMainActivity.class);
				superActivity.startActivity(intent);
				superActivity.finish();
			}
		}
		else
		{
			SharedPreferencesUtil.setValue(superActivity,
					SharedKey.Shared_Mobile_Key, mobile);
			SharedPreferencesUtil.setValue(superActivity,
					SharedKey.Shared_UserId_Key, userId);
			saveWeiboInfo((Context)superActivity, loginType, weiboId, weiboName);
			LoadDataBus loadData = new LoadDataBus((Context)superActivity, (ILoadBus) this);
			loadData.loadData(mobile);
		}
	}
	
	public boolean checkWeiboLogin() {
		boolean result = false;
		try {
			String baseUrl = getUrl();
			baseUrl = baseUrl + "?uid=" + weiboId;
			BaseResp br = HttpRequest.DoHttpRequest(baseUrl);

			if (br.isSuccess()) {
				JSONObject jsonObject = new JSONObject(br.getHttpResult());
				JSONObject jsonResponse = jsonObject.getJSONObject("response");
				userId = jsonResponse.getString("userId");
				mobile = jsonResponse.getString("mobile");
				result = true;
				
			}
		} catch (Exception e) {
			result = false;
			Log.e(e.getMessage(), e);
		} finally {
		}

		return result;
	}
	
	@Override
	protected void onPostExecute(Integer response) {
		if(processtype == 1)
		{
			Toast.makeText(superActivity, weiboTitle+"授权失败。", Toast.LENGTH_SHORT).show();
			dismissProgress();
		}
	};
	
	public void doQAuthResultTask()
	{
		boolean result = onQAuthResult(data);
		if(!result)
		{
			processtype = 1;
			return;
		}
		
		result = checkWeiboLogin();
		dismissProgress();
		if(!result)
		{
			Intent intent=new Intent(superActivity,RegisterActivity.class);
			intent.putExtra("modifytype",2);
			intent.putExtra("loginType",loginType);
			intent.putExtra("weiboName",weiboName);
			intent.putExtra("weiboHeader",weiboHeader);
			intent.putExtra("weiboId",weiboId);
			intent.putExtra("weiboTitle",weiboTitle);
			superActivity.startActivity(intent);
		}
		else
		{
			SharedPreferencesUtil.setValue(superActivity,
					SharedKey.Shared_Mobile_Key, mobile);
			SharedPreferencesUtil.setValue(superActivity,
					SharedKey.Shared_UserId_Key, userId);
			saveWeiboInfo((Context)superActivity, loginType, weiboId, weiboName);
			LoadDataBus loadData = new LoadDataBus((Context)superActivity, (ILoadBus) this);
			loadData.loadData(mobile);
		}
	}
	
	public void onCompleted(boolean isSuccess, String desc) {
		if(isShowProcess){
			doLoginComplete(isSuccess, desc);
		}
		if(isSuccess)
		{
			//Toast.makeText(superActivity, "登录成功", Toast.LENGTH_SHORT).show();
			Intent intent = new Intent(superActivity, CoachSiteMainActivity.class);
			superActivity.startActivity(intent);
			superActivity.finish();
		}
		else
		{
			SharedPreferencesUtil.setValue(superActivity,
					SharedKey.Shared_Mobile_Key, "");
			
			SharedPreferencesUtil.setValue(superActivity,
					SharedKey.Shared_UserId_Key, "");
			Toast.makeText(superActivity, "登录失败", Toast.LENGTH_SHORT).show();
			Intent intent = new Intent(superActivity, CoachSiteMainActivity.class);
			superActivity.startActivity(intent);
			superActivity.finish();
		}
	}

	private void doLoginComplete(boolean isSuccess, String desc) {
		dismissProgress();

		String toaskInfo = successInfo;
		if (!isSuccess) {
			toaskInfo = errorInfo + "," + desc;
		}
		//Toast.makeText(superActivity, toaskInfo, Toast.LENGTH_SHORT).show();
		
		if(!isSuccess && !isShowProcess)
		{
			SharedPreferencesUtil.setValue(superActivity,
					SharedKey.Shared_UserId_Key, "");
			Intent intent = new Intent(superActivity, CoachSiteMainActivity.class);
			//Toast.makeText(superActivity, "登陆失败", Toast.LENGTH_SHORT).show();
			//Intent intent = new Intent(superActivity, LoginActivity.class);
			superActivity.startActivity(intent);
			superActivity.finish();
		}
	}
	
	public void doQAuthResult(Intent data)
	{
		showProgress();
		this.data = data;
		this.execute();
	}
	
	protected void showProgress() {
		if (null == mProgress) {
			mProgress = new ProgressDialog(superActivity);
			mProgress.setMessage(processInfo);
		}
		mProgress.show();
	}

	protected void dismissProgress() {
		if (null != mProgress) {
			mProgress.dismiss();
			mProgress = null;
		}
	}
}
